package com.weilan.ark3.utils;

import java.math.BigInteger;
import java.util.Locale;

/**
 * @description 十六进制转化工具类
 * @date 2023/4/2 22:25
 */

public class HexUtils {
    /**
     * 返回两个16进制之间的长度
     *
     * @param firstHex 减数
     * @param lastHex  被减数
     * @return 差
     */
    public static int subHex(String firstHex, String lastHex) {
        //减数
        int subtraction = Integer.parseInt(firstHex.substring(2), 16);
        //被减数
        int minuend = Integer.parseInt(lastHex.substring(2), 16);
        return minuend - subtraction + 1;
    }


    public static Integer timeToDEC(String inputString) {
        // 拆分时分字符串
        String[] parts = inputString.split(":");
        if (parts.length == 2) {
            String highByteBinary = convertToBinary(parts[0]); // 转换高八位为二进制
            String lowByteBinary = convertToBinary(parts[1]);  // 转换低八位为二进制

            // 将高八位和低八位的二进制拼接
            String concatenatedBinary = highByteBinary + lowByteBinary;

            // 将拼接后的二进制字符串转换为十进制数
            return Integer.parseInt(concatenatedBinary, 2);

//            System.out.println("输入的时分字符串: " + inputString);
//            System.out.println("左边高八位的二进制表示: " + highByteBinary);
//            System.out.println("右边低八位的二进制表示: " + lowByteBinary);
//            System.out.println("拼接后的二进制表示: " + concatenatedBinary);
//            System.out.println("转换后的十进制值: " + decimalValue);
//            System.out.println();
        }
        return -1;
    }

    /**
     * 16进制转化为10进制
     *
     * @param hex 16进制数
     * @return 10进制数
     */
    public static int hexToDec(String hex) {
        return Integer.parseInt(hex.substring(2), 16);
    }

    /**
     * 10进制转化为16进制
     *
     * @param dec 10进制数
     * @return 16进制数
     */
    public static String decToHex(Integer dec) {
//        String upperCase = new String(Integer.toHexString(dec)).toUpperCase(Locale.ROOT);
//        return "0x" + upperCase;
        String upperCase = String.format("%0" + 4 + "x", dec).toUpperCase(Locale.ROOT);
        return "0x" + upperCase;
    }


    /*
     * 将10进制转化为16进制的数，且均为4位
     * */
    public static String dec2Hex(Integer dec) {
        String upperCase = String.format("%0" + 4 + "x", dec).toUpperCase(Locale.ROOT);
        return "0x" + upperCase;
        //String.format("%0" + 4 + "x", dec);
    }


    /**
     * 十进制转二进制（String）
     */
    public static String decimalToBinary(String decimal) {
        BigInteger num = new BigInteger(decimal);
        StringBuilder binary = new StringBuilder();

        while (num.compareTo(BigInteger.ZERO) > 0) {
            BigInteger[] quotientAndRemainder = num.divideAndRemainder(BigInteger.valueOf(2));
            binary.insert(0, quotientAndRemainder[1]);
            num = quotientAndRemainder[0];
        }

        return binary.toString();
    }


    //有符号整型转32位单精度浮点型
    public static float signedTo32FloatToPower(int high, int low) {
        int highResult = high;
        int lowResult = low;
        if (high < 0) {
            highResult = high & 0xFFFF;
        }
        if (low < 0) {
            lowResult = low & 0xFFFF;
        }
        int combined = (highResult << 16) | lowResult;
        float intBitsToFloat = Float.intBitsToFloat(combined);
        float v = intBitsToFloat / 1000;
        return v;

    }

    public static String convertToBinary(String decimalString) {
        int decimalValue = Integer.parseInt(decimalString);
        String binaryString = Integer.toBinaryString(decimalValue);

        // 补零，确保二进制表示是八位
        while (binaryString.length() < 8) {
            binaryString = "0" + binaryString;
        }

        return binaryString;
    }


    /*
     * 处理起始时间结束时间
     * */
    public static String timeTrans(int time){
        //将数据转换为2进制
        String s = decimalToBinary(String.valueOf(time));
        //补齐0
        String newT = StringUtils.leftPadString(s);
        //截取前8 后8位
        String gao = newT.substring(0, newT.length() - 8);
        String di = newT.substring(newT.length() - 7, newT.length());
        int shi = binaryToDecimal(gao);
        int fen = binaryToDecimal(di);
        String tm = String.valueOf(shi)+":"+String.valueOf(fen);
        return tm;
    }
    /**
     * 二进制转十进制
     */
    public static int binaryToDecimal(String binary) {
        int decimal = 0;
        int power = 0;
        for (int i = binary.length() - 1; i >= 0; i--) {
            if (binary.charAt(i) == '1') {
                decimal += Math.pow(2, power);
            }
            power++;
        }
        return decimal;
    }


    public static short Power(int a, int b, int c, int d, int e, int f) {
        if (a == 0 && b == 0 && c == 0 && d == 0 && e == 0 && f == 0) {
            return 0;
        }
        float l1 = HexUtils.signedTo32FloatToPower(a, b);
        float l2 = HexUtils.signedTo32FloatToPower(c, d);
        float l3 = HexUtils.signedTo32FloatToPower(e, f);
        short result = (short) ((l1 + l2 + l3) * 10);
        return result;

    }


    /*
     * 安科瑞功率电表的功率计算
     * a:功率值
     * b:电压变比
     * c:电流变比
     * */
    public static short AnPower(int a, int b, int c, int d) {
        //转高低位
        int data = (a * 65536) + b;
        return (short) ((data * c * d) * 0.001);

    }
}
